package com.alphaking.service.mingweicai;

import java.util.List;
import java.util.UUID;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;

import com.alphaking.constant.CategoryConstant;
import com.alphaking.constant.SortConstant;
import com.alphaking.model.Category;
import com.alphaking.pojo.common.PagerResult;
import com.alphaking.pojo.common.SearchBean;
import com.alphaking.pojo.common.SearchCondition;
import com.alphaking.service.base.ImportDaoService;

@Service
@Transactional
public class CategoryService extends ImportDaoService {

	/**
	 * 查询第一级类别
	 * 
	 * @param searchCondition
	 * @throws SecurityException
	 * @throws NoSuchFieldException
	 */
	public void findCategorys(Model model) throws NoSuchFieldException, SecurityException {
		// 父级id
		SearchCondition searchCondition = SearchCondition.addSearchBeanByMap(null, "parentCategoryId",
				SearchBean.OPERATOR_IN, CategoryConstant.ONE_LEVEL_PARENTID + "", null);
		// 非删除
		SearchCondition.addSearchBeanByMap(searchCondition, "isDeleted", SearchBean.OPERATOR_IN,
				CategoryConstant.ISDELETED_FALSE + "", null);
		PagerResult<Category> pagerResult = this.productCategoryDao.listModel(false, searchCondition);
		model.addAttribute("pagerResult", pagerResult);
		model.addAttribute("searchCondition", searchCondition);

	}

	/**
	 * 添加一级分类
	 * 
	 * @param json
	 */
	public void addCategory(String productCategoryName, String productCategoryUrl) {

		System.out.println(productCategoryUrl);
		// 保存第一级分类
		Category productCategory = new Category();
		productCategory.setProductCategoryId(UUID.randomUUID().toString());
		System.out.println(productCategory.getProductCategoryId());
		productCategory.setProductCategoryName(productCategoryName);
		productCategory.setParentCategoryId("0");
		productCategory.setIsHidden(CategoryConstant.PRODUCT_CATEGORY_IS_NOT_HIDDEN);
		productCategory.setIsUnknow(CategoryConstant.ISUNKNOW_FALSE);
		productCategory.setIsDeleted(CategoryConstant.ISDELETED_FALSE);
		this.productCategoryDao.save(productCategory);
        
	}


	/**
	 * 更新分类
	 * 
	 * @param json
	 */
	public void updateCategory(String productCategoryId, String productCategoryName, String productCategoryUrl) {

		Category productCategory = this.productCategoryDao.getById(productCategoryId);
		productCategory.setProductCategoryName(productCategoryName);
		this.productCategoryDao.update(productCategory);
	}


	/**
	 * 隐藏或者显示一级分类
	 * 
	 * @param id
	 * @throws SecurityException
	 * @throws NoSuchFieldException
	 */
	public void hideOrShowFirstCategory(String id) throws NoSuchFieldException, SecurityException {

		// 删除第一级分类
		Integer hideOrShow = 0;
		Category productCategory = this.productCategoryDao.getById(id);
		if (productCategory.getIsHidden() == CategoryConstant.PRODUCT_CATEGORY_IS_HIDDEN) {
			productCategory.setIsHidden(CategoryConstant.PRODUCT_CATEGORY_IS_NOT_HIDDEN);
			hideOrShow = CategoryConstant.PRODUCT_CATEGORY_IS_NOT_HIDDEN;
		} else if (productCategory.getIsHidden() == CategoryConstant.PRODUCT_CATEGORY_IS_NOT_HIDDEN) {
			productCategory.setIsHidden(CategoryConstant.PRODUCT_CATEGORY_IS_HIDDEN);
			hideOrShow = CategoryConstant.PRODUCT_CATEGORY_IS_HIDDEN;
		}
		this.productCategoryDao.saveOrUpdate(productCategory);

	}


	/**
	 * 改变次序
	 * 
	 * @param firsrId
	 * @param secondId
	 */
	public void changeOrder(String firstId, String secondId) {

		Category firstCategory = this.productCategoryDao.getById(firstId);
		Category secondCategory = this.productCategoryDao.getById(secondId);
//		Integer firstOrder = firstCategory.getPriority();
//		Integer secondOrder = secondCategory.getPriority();
//
//		firstCategory.setPriority(secondOrder);
//		this.productCategoryDao.saveOrUpdate(firstCategory);
//
//		secondCategory.setPriority(firstOrder);
//		this.productCategoryDao.saveOrUpdate(secondCategory);

	}

	/**
	 * 找到下级分类
	 * 
	 * @param parentId
	 * @throws SecurityException
	 * @throws NoSuchFieldException
	 */
	public void findCategoriesByparent(String parentId, Model model) throws NoSuchFieldException, SecurityException {

		// 父级id
		SearchCondition searchCondition = SearchCondition.addSearchBeanByMap(null, "parentCategoryId",
				SearchBean.OPERATOR_IN, parentId + "", null);
		// 非删除
		SearchCondition.addSearchBeanByMap(searchCondition, "isDeleted", SearchBean.OPERATOR_IN,
				CategoryConstant.ISDELETED_FALSE + "", null);
		// 按照是否未知排序
		SearchCondition.addSearchBeanByMap(searchCondition, "isUnknow", SearchBean.OPERATOR_SORT, "desc", 0);
		// 优先级排序
		SearchCondition.addSearchBeanByMap(searchCondition, "priority", SearchBean.OPERATOR_SORT, "asc", 1);
		PagerResult<Category> pagerResult = this.productCategoryDao.listModel(false, searchCondition);
		model.addAttribute("pagerResult", pagerResult);
		model.addAttribute("searchCondition", searchCondition);

	}

	/**
	 * 获取到商品二级分类
	 * 
	 * @return
	 * @throws SecurityException
	 * @throws NoSuchFieldException
	 */
	public List<Category> getPriorityCategory(Long parentId) throws NoSuchFieldException, SecurityException {

		// 获取到查询条件
		SearchCondition searchCondition = getCategorySearchCondition(parentId);
		// 进行查询
		List<Category> productCategorys = this.productCategoryDao.listModel(false, searchCondition).getResultSet();
		return productCategorys;
	}

	// 获取到查询category的条件
	public SearchCondition getCategorySearchCondition(Long parentId) {
		// 父级id
		SearchCondition searchCondition = SearchCondition.addSearchBeanByMap(null, "parentCategoryId",
				SearchBean.OPERATOR_IN, parentId + "", null);
		// 非删除
		SearchCondition.addSearchBeanByMap(searchCondition, "isDeleted", SearchBean.OPERATOR_IN,
				CategoryConstant.ISDELETED_FALSE + "", null);
		// 非隐藏
		SearchCondition.addSearchBeanByMap(searchCondition, "isHidden", SearchBean.OPERATOR_IN,
				CategoryConstant.PRODUCT_CATEGORY_IS_NOT_HIDDEN + "", null);
		// 优先级排序
		SearchCondition.addSearchBeanByMap(searchCondition, "priority", SearchBean.OPERATOR_SORT, SortConstant.SHENGXU,
				0);
		return searchCondition;
	}

	/**
	 * 删除一级分类
	 * 
	 * @param id
	 * @throws SecurityException
	 * @throws NoSuchFieldException
	 */
	public void deleteFirstCategory(String id) throws NoSuchFieldException, SecurityException {

		// 删除第一级分类
		Category productCategory = this.productCategoryDao.getById(id);
		productCategory.setIsDeleted(CategoryConstant.ISDELETED_TRUE);
		this.productCategoryDao.saveOrUpdate(productCategory);

	}


}
